﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Common;
namespace DAO
{
    public class DataProvider
    {
        private SqlConnection cnn = null;
        private SqlDataAdapter da = null;
        SqlCommand cmd = null;
        DataTable dt = null;      
        public DataTable LayDuLieuThamSo(string sql, CommandType command, params object[] par)
        {
            dt = new DataTable();
            cnn = Connect.Connected();
            cmd = new SqlCommand(sql, cnn);
            cmd.CommandType = command;
            for (int i = 0; i < par.Length; i += 2)
            {
                SqlParameter para = new SqlParameter(par[i].ToString(), par[i + 1]);
                cmd.Parameters.Add(para);
            }
            da = new SqlDataAdapter(cmd);
            dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public bool ExecuteQuery(string sql, CommandType command, params object[] par)
        {
            cnn = Connect.Connected();
            int numRecordsEffect = 0;
            try
            {
               
                cmd = new SqlCommand(sql, cnn);
                cmd.CommandType = command;
                for (int i = 0; i < par.Length; i += 2)
                { 
                    SqlParameter para = new SqlParameter(par[i].ToString(),par[i+1]);
                    cmd.Parameters.Add(para);
                }
                numRecordsEffect = cmd.ExecuteNonQuery();
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
            if (numRecordsEffect > 0)
                return true;
            return false;
        }

    }
    
}